package com.chandra.pos.order.dao.entity;

import com.chandra.pos.model.PosEntity;
import com.chandra.pos.product.dao.entity.Product;

import javax.persistence.*;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created with IntelliJ IDEA.
 * Customer: Admin
 * Date: 10/7/13
 * Time: 1:28 AM
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name="ORDER_ITEMS")
public class OrderItem extends PosEntity {



    @Column(name = "PRODUCT_SALE_PRICE")
    private BigDecimal salePrice;

    @Column(name = "ORDER_QUANTITY")
    private BigDecimal quantity;

    @Column(name = "NOTE")
    private String note;

    @Column(name = "DISCOUNT")
    private BigDecimal discount;

    @Column(name = "ORDER_ID")
    private Long orderId;


    @OneToOne
    @JoinColumn(name = "PRODUCT_ID")
    private Product product;

    public BigDecimal getSalePrice() {
        return salePrice;
    }

    public void setSalePrice(BigDecimal salePrice) {
        this.salePrice = salePrice;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    @Transient
    public BigDecimal getTotalAmount() {
        BigDecimal totalAmount = new BigDecimal(0);


        if (getQuantity() != null && getSalePrice() != null) {
            BigDecimal item_amount_obj = this.getSalePrice().multiply(this.getQuantity());
            totalAmount = totalAmount.add(item_amount_obj);

        }


        if (discount != null)
            totalAmount = totalAmount.subtract(discount);


        totalAmount = totalAmount.setScale(Order.AMOUNT_PRECISION, RoundingMode.HALF_UP);

        return totalAmount;

    }

}
